/*--------------------------- MegaWave2 Module -----------------------------*/
/* mwcommand
  name = {hugeFview};
  version = {"1.8"};
  author = {"Baptiste Coulange"};
  function = {"fview avec options de zoom poussées"};
  usage = { 
  	'z':[zoom=1.0]->zoom  "Zoom factor (image 1) [si aucune option de zoom, zoom de l'image 2 pour avoir la meme taille que l'image 2]",	
	'Z':[zoom2=1.0]->zoom2  "Zoom factor (image 2)",	
	's'->sameSize			"Forcer la meme taille d'affichage",
    'o':[order=0]->order  "Zoom order: 0,1=linear,-3=cubic,3,5..11=spline,12=Fourier",
	'd':dee->dee			"discard d percent of extremal values",
	'n':[nZoomX=512]->nZoomX  "taille du coté de l'image de zoom",
	'N':[nZoomY=512]->nZoomY  "taille du coté de l'image de zoom",
	'v':tailImp->tailImp  "forcer la taille de l'affichage principal (max des deux longueurs)",
	in->in            "Fimage d'entrée",
	'i':in2->in2		  "Fimage for flip",
	 notused->window       			"Window to view the image (internal use)"
          };
*/
/*----------------------------------------------------------------------
 1.0 : initial version
 1.1 : ajout du flip
 1.2 : corrections de bugs diverses
 1.3 : size ajustement
 1.4 : fftzoom option
 1.5 : correction de bug
 1.6 : ajout d'option d'affichage et nettoyage des headers
 1.7 : forcer à une taille humaine avec -v
 1.7.1 : Display bug correction
 1.7.2 : Display bug correction
 1.8 : Dynamic zoom
----------------------------------------------------------------------*/

#include <string.h>
#include <stdio.h>
#include <time.h>
#include "mw.h"
#include <math.h>
#define MAX(x,y) ((x)>(y)?(x):(y))
#define MIN(x,y) ((x)>(y)?(y):(x))
#define MMIN(x,y,a,b) MIN(MIN(x,y),MIN(a,b))
#define MMAX(x,y,a,b) MAX(MAX(x,y),MAX(a,b))


/* Include the window since we use windows facility */
#include "window.h"


extern void fzoom();
extern fthre();
extern void splot3();
extern void fline_extract();
extern fftzoom();





/* Param structure used to send parameters to fview_notify() */

typedef struct hfv_SParam {
	/* Cimages affichées */
	Cimage imageDisp;
	Cimage imaZoomDisp;			/* Zoom Local									*/

	/* Version Fimage des affichées */
	Fsignal section,subsection;								/*Signaux affichés								*/
	Fsignal section2,subsection2;								/*Signaux affichés								*/

	/* Témoins de l'interface et de l'affichage */
	int Reafficher;											/*flag du réaffichage							*/
	int ima1,deuxIm;
	int GLprint,GLprintZ;

	/* Pour le zoom local */
	int posX1,posY1,AposX1,AposY1,pasFiltres,avX,avY;		/*pour la zone de filtrage						*/	  	 
	int zoomX1,zoomY1,zoomX12,zoomY12;
	float 		coeffzoom,coeffzoom2;		/*Parametres sur zoom local						*/		
	int nZoomX,nZoomY;
	Fimage fZoom,fZoom2;
	int ReafficherZ;
	int recalZoo;
	int dynRecalZoo,debutfix;
	int tailChange;
	time_t debutZoomDym,finZoomDym;
	
	

	/* Fimages utilisées */
	Fimage imageRe,imageReN,imWind;							/* Fimage d'entrée  							*/
	Fimage imageRe2,imageReN2,imWind2;						/* Fimage d'entrée  							*/


	/* Autres parametres */
	int nx,ny,nx2,ny2;										/* taille de l'image de départ					*/
	float zoomOr,zoomOr2;	
	int tailX,tailY;
	int ZoomVerouil;

	/* Sorties récupérables */
	Fimage oute,oute2,oute3,oute4,oute5;					/*Sorties										*/

	/* Fenetres affichées */
	Wframe *ImageWindow;									/* fenêtre principale							*/
	Wframe *PlotWindow;										/* Plot de la fenetre principale 				*/
	Wframe *SubPlotWindow;									/* Plot du zoom 								*/
	Wframe *ZoomWindow;  									/* Fenetre du zoom								*/

	
	
} *hfv_Param;


static int oldx1,oldy1,oldevent,cscale_shown;
static float zfactor;

/*#include "/home/megawave/Bureau/my_megawave2/src/eye/eye_zonFourLoc3_splot.h"*/



/* Conversion en Cimage */
static void f2cimageLoc(in,out)
     Fimage in;
     Cimage out;
{
	int n;
	float v;



	out = mw_change_cimage(out,in->nrow,in->ncol);


	for (n=in->ncol*in->nrow;n--;) {
    	v = in->gray[n];
    	if (v<0.) v=0.;
    	if (v>255.) v=255.;
    	out->gray[n] = (unsigned char)v;
 	}


 
}







/* Affichage */
static void Affichage(in,nx,ny,ImageWindowLoc)
	Cimage in;
	int nx,ny;
	Wframe *ImageWindowLoc;

{
  WLoadBitMapImage(ImageWindowLoc,in->gray,in->ncol,in->nrow); 
  WRestoreImageWindow(ImageWindowLoc,0,0,nx,ny); 
  WFlushAreaWindow(ImageWindowLoc,0,0,nx,ny);
}






static void hfv_notify_help()

{
  printf("\n\t\tHelp on line\n");

  printf("\nMouse:actions différentes en fonction de la zone de clic : \n");
  printf("\tGauche : affichage des valeurs de pixels \n");


  printf("\nKeyboard:\n");

	printf("\tSpace: Flip si plusieurs images.\n");
	printf("\tl: Affichage de la ligne courante.\n");
	printf("\tc: Affichage de la colone courante.\n");
	printf("\tz: zoom de la zone pointée.\n");
	printf("\tQ: Quit.\n");
  printf("\tH: Help.\n");

}

static void zoom_notify_help()

{
  printf("\n\t\tHelp on line\n");

  printf("\nMouse:\n");
  printf("\tLeft button: unzoom.\n");
  printf("\tRight button: zoom.\n");


  printf("\nKeyboard:\n");

	printf("\tl: Affichage de la ligne courante.\n");
	printf("\tc: Affichage de la colone courante.\n");
	printf("\tUp: déplacement de la zone zoomée.\n");
	printf("\tLeft: déplacement de la zone zoomée.\n");
	printf("\tRight: déplacement de la zone zoomée.\n");
	printf("\tDown: déplacement de la zone zoomée.\n");
	printf("\tv : verouillage ou deverouillage de la zone de zoom (si deverouille, les zones des deux images coincident\n");
	printf("\td : affichage des informations de pixel\n");
	printf("\tQ: Quit.\n");
  printf("\tH: Help.\n");

}


static void reaffXY(param)
	void *param;
{
	/* Initialisations */

  	hfv_Param images;
  	images = (hfv_Param) param;  /* Cast */	
	int nx,ny,nx2,ny2;

	nx=images->imageRe->ncol;
	ny=images->imageRe->nrow;
	images->zoomX1=images->zoomX1>nx?nx:(images->zoomX1<0?0:images->zoomX1);
	images->zoomY1=images->zoomY1>ny?ny:(images->zoomY1<0?0:images->zoomY1);

	if(images->deuxIm==1){
	nx2=images->imageRe2->ncol;
	ny2=images->imageRe2->nrow;
	images->zoomX12=images->zoomX12>nx2?nx2:(images->zoomX12<0?0:images->zoomX12);
	images->zoomY12=images->zoomY12>ny2?ny2:(images->zoomY12<0?0:images->zoomY12);
	}

}



static void petitZoom(param)
	void *param;
{

	/* Initialisations */

  	hfv_Param images;
  	images = (hfv_Param) param;  /* Cast */	
	Fimage AvtZoomR;



	int x1,y1,x,y,nx,ny,nx2,ny2,adrss;
	float fcoeffzoom,dee;
	char flag;
if(images->ima1){
	nx=images->imageRe->ncol;
	ny=images->imageRe->nrow;
	x1=images->zoomX1;
	y1=images->zoomY1;
	fcoeffzoom=(float)images->coeffzoom;

	AvtZoomR=mw_change_fimage(NULL,(int)rint(images->nZoomY/fcoeffzoom),(int) rint(images->nZoomX/fcoeffzoom));
	images->fZoom=mw_change_fimage(images->fZoom,images->nZoomY,images->nZoomX);
	mw_clear_fimage(images->fZoom,0.);	
}else{
	nx=images->imageRe2->ncol;
	ny=images->imageRe2->nrow;
	x1=images->zoomX12;
	y1=images->zoomY12;	
	fcoeffzoom=(float)images->coeffzoom2;
	AvtZoomR=mw_change_fimage(NULL,(int) rint(images->nZoomY/fcoeffzoom),(int) rint(images->nZoomX/fcoeffzoom));
	images->fZoom=mw_change_fimage(images->fZoom,images->nZoomY,images->nZoomX);
	mw_clear_fimage(images->fZoom2,0.);
}	
	

	dee=1.;
	mw_clear_cimage(images->imaZoomDisp,0);
	mw_clear_fimage(AvtZoomR,0.);



		for(x=0;x<(int)AvtZoomR->ncol;x++)
			for(y=0;y<(int)AvtZoomR->nrow;y++){
			/* Zoom de l'image de Gauche */

		if(y1-(int) images->nZoomY/(2*fcoeffzoom)+y>=0
		 &&x1-(int) images->nZoomX/(2*fcoeffzoom)+x>0
		 &&y1-(int) images->nZoomY/(2*fcoeffzoom)+y<ny
		 &&x1-(int) images->nZoomX/(2*fcoeffzoom)+x<nx){
				adrss=(y1-(int)(images->nZoomY/(2*fcoeffzoom))+y)*(nx)+(x1-(int)(images->nZoomX/(2*fcoeffzoom))+x);
		if(images->ima1){
			AvtZoomR->gray[y*(int)rint(images->nZoomX/fcoeffzoom)+x]=images->imageReN->gray[adrss];
		}else{
			AvtZoomR->gray[y*(int)rint(images->nZoomX/fcoeffzoom)+x]=images->imageReN2->gray[adrss];}
		}


	}


	if(images->ima1){
		fftzoom(AvtZoomR,images->fZoom,&fcoeffzoom,NULL);
		f2cimageLoc(images->fZoom,images->imaZoomDisp);
	}else{
		fftzoom(AvtZoomR,images->fZoom2,&fcoeffzoom,NULL);
		f2cimageLoc(images->fZoom2,images->imaZoomDisp);
	}


/* Free Memory */
if(AvtZoomR)mw_delete_fimage(AvtZoomR);


}

static void dessinerZoneZoom(ImageWindow,param)
	void *param;
	Wframe *ImageWindow;
{
	/* Initialisations */

  	hfv_Param images;
  	images = (hfv_Param) param;  /* Cast */	
	int x1,y1,nx,ny;
	float coeffloc;
if(images->ima1){
	nx=images->imWind->ncol;
	ny=images->imWind->nrow;
	x1=images->zoomX1*images->zoomOr;
	y1=images->zoomY1*images->zoomOr;
	coeffloc=2*images->coeffzoom/images->zoomOr;
}else{
	nx=images->imWind->ncol;
	ny=images->imWind->nrow;
	x1=images->zoomX12*images->zoomOr2;
	y1=images->zoomY12*images->zoomOr2;	
	coeffloc=2*images->coeffzoom2/images->zoomOr2;
}
	
	      WSetTypePencil(W_COPY);
	      WSetSpecialColorPencil(ImageWindow);

		  Affichage(images->imageDisp,images->imageDisp->ncol,images->imageDisp->nrow,ImageWindow);

	      

WDrawLine(ImageWindow,x1-(int) (images->nZoomX/(coeffloc))>0?x1-(int) (images->nZoomX/(coeffloc)):0,
					  y1-(int) (images->nZoomY/(coeffloc))>0?y1-(int) (images->nZoomY/(coeffloc)):0,
					  x1-(int) (images->nZoomX/(coeffloc))>0?x1-(int) (images->nZoomX/(coeffloc)):0,
					  y1+(int) images->nZoomY/(coeffloc)>ny?ny:y1+(int) images->nZoomY/(coeffloc)); 
	      WDrawLine(ImageWindow,x1-(int) images->nZoomX/(coeffloc)>0?x1-(int) images->nZoomX/(coeffloc):0,
								y1-(int) images->nZoomY/(coeffloc)>0?y1-(int) images->nZoomY/(coeffloc):0,
								x1+(int) images->nZoomX/(coeffloc)<nx?x1+(int) images->nZoomX/(coeffloc):nx,
								y1-(int) images->nZoomY/(coeffloc)>0?y1-(int) images->nZoomY/(coeffloc):0); 
	      WDrawLine(ImageWindow,x1-(int) images->nZoomX/(coeffloc)>0?x1-(int) images->nZoomX/(coeffloc):0,
								y1+(int) images->nZoomY/(coeffloc)>ny?ny:y1+(int) images->nZoomY/(coeffloc),
								x1+(int) images->nZoomX/(coeffloc)<nx?x1+(int) images->nZoomX/(coeffloc):nx,
								y1+(int) images->nZoomY/(coeffloc)>ny?ny:y1+(int) images->nZoomY/(coeffloc));  
	      WDrawLine(ImageWindow,x1+(int) images->nZoomX/(coeffloc)<nx?x1+(int) images->nZoomX/(coeffloc):nx,
								y1-(int) images->nZoomY/(coeffloc)>0?y1-(int) images->nZoomY/(coeffloc):0,
								x1+(int) images->nZoomX/(coeffloc)<nx?x1+(int) images->nZoomX/(coeffloc):nx,
								y1+(int) images->nZoomY/(coeffloc)>ny?ny:y1+(int) images->nZoomY/(coeffloc)); 
		

	      WFlushAreaWindow(ImageWindow,0,0,images->imageDisp->ncol,images->imageDisp->nrow);	  
	      cscale_shown = 0;
}

/* NOtify pour la fenetre du zoom */

static int zoom_notify(ImageWindow,param)

Wframe *ImageWindow;
void *param;          /* Users's parameters: don't forget the cast ! */

{
	/* Initialisations */
	int x1,y1,event,button_mask,ret,c,nx,ny;
	int vraiX,vraiY;
	float ng;
	char mess[80];
  	hfv_Param images;
  	char flag;
	int x0=0;
  	int y0=0;
  	images = (hfv_Param) param;  /* Cast */	
	nx=images->imaZoomDisp->ncol;
	ny=images->imaZoomDisp->nrow;
	

	/* For section */
  	int sx=500;
  	int sy=200;
  	char cflag=1;
  	int norefresh=1;

	if(images->dynRecalZoo==1){
	/* Draw zoom place */		
			dessinerZoneZoom(images->ImageWindow,images);
	/* Calc zoom */
			petitZoom(images);
			images->dynRecalZoo==0;
}

	if(images->ReafficherZ==1){
		Affichage(images->imaZoomDisp,images->nZoomX,images->nZoomY,ImageWindow);	
		images->ReafficherZ=0;

	}

	/* Captation de l'evenement du moment */
  	event = WUserEvent(ImageWindow); /* User's event on ImageWindow */	
  	if (event < 0) ret=1; else ret=event;
	

	if (event != W_DESTROY)
     {
	    WGetStateMouse(ImageWindow,&x1,&y1,&button_mask);	 
}


      if (images->GLprintZ == 1)  
	{
	if(images->ima1){
		vraiX=(int)((x1-images->nZoomX/(2.))/images->coeffzoom+images->zoomX1);
		vraiY=(int)((y1-images->nZoomY/(2.))/images->coeffzoom+images->zoomY1);	
	  if ((vraiX>=0)&&(vraiX<images->imageRe->ncol)
		&&(vraiY>=0)&&(vraiY<images->imageRe->nrow))
	    {
	      ng = images->imageRe->gray[ vraiY + (images->imageRe->ncol)* vraiX ];
	      sprintf(mess," (%3d,%3d): %f ",vraiX,vraiY,ng);
	    }
	  else sprintf(mess," (%3d,%3d): ----------",vraiX,vraiY);
	}else{
		vraiX=(int)(x1/images->coeffzoom2-images->nZoomX/(2*images->coeffzoom2)+images->zoomX12);
		vraiY=(int)(y1/images->coeffzoom2-images->nZoomY/(2*images->coeffzoom2)+images->zoomY12);	
	  if ((vraiX>=0)&&(vraiX<images->imageRe2->ncol)
		&&(vraiY>=0)&&(vraiY<images->imageRe2->nrow))
	    {
	      ng = images->imageRe2->gray[ vraiY + (images->imageRe2->ncol)* vraiX ];
	      sprintf(mess," (%3d,%3d): %f ",vraiX,vraiY,ng);
	    }
	  else sprintf(mess," (%3d,%3d): ----------",vraiX,vraiY);
	}
	  WDrawString(ImageWindow,0,10,mess);
	  /*usleep(100);*/
	if(images->ima1){
	  sprintf(mess,"Zoom : x%f",images->coeffzoom);
	}else{
	  sprintf(mess,"Zoom : x%f",images->coeffzoom2);
	}
	  WDrawString(ImageWindow,0,24,mess);
	  WFlushAreaWindow(ImageWindow,0,0,images->nZoomX-1,24);	

	}

	if(images->tailChange==1&&
		images->nZoomY == images->ZoomWindow->dy&&
		images->nZoomX == images->ZoomWindow->dx){
		if(images->debutfix==0)	{images->debutZoomDym=time(NULL);images->debutfix=1;	
		}else{images->finZoomDym=time(NULL);
			if(difftime(images->finZoomDym,images->debutZoomDym)>2.){
      		images->recalZoo=1;
	  		images->ReafficherZ=1;
	  		images->dynRecalZoo=1;
	  		images->tailChange=0;
			images->debutfix==0;
			}
		}
	}
	/* Gestion de tous les évènements */					   
  switch(event)
    {


   case W_RESIZE:

	  images->tailChange=1;

	if(images->nZoomX != images->ZoomWindow->dx||images->nZoomY != images->ZoomWindow->dy){
		images->debutfix=0;
		images->nZoomX = images->ZoomWindow->dx; images->nZoomY = images->ZoomWindow->dy;
	}
     
      break;

	case W_MS_LEFT:
	if((images->ima1&&images->coeffzoom>1.)||((!images->ima1)&&images->coeffzoom2>1.)){
	if(images->ima1||(!images->ZoomVerouil)) images->coeffzoom=images->coeffzoom/2.;
	if((!images->ima1)||(!images->ZoomVerouil)) images->coeffzoom2=images->coeffzoom2/2.;

	/* Draw zoom place */		
		dessinerZoneZoom(images->ImageWindow,images);
	
	/* Compute image */
	 
		petitZoom(images);
		
		if(!images->ZoomVerouil==1){images->recalZoo=1;}
		
		images->ReafficherZ=1;
	}
		break;

	case W_MS_RIGHT:
	if(images->coeffzoom<(images->nZoomX<images->nZoomY?images->nZoomX:images->nZoomY)/4){
	if(images->ima1||(!images->ZoomVerouil)) images->coeffzoom=2.*images->coeffzoom;
	if((!images->ima1)||(!images->ZoomVerouil)) images->coeffzoom2=2.*images->coeffzoom2;

	/* Draw zoom place */		
			dessinerZoneZoom(images->ImageWindow,images);
	
	/* Compute image */
	 
		petitZoom(images);

		if(!images->ZoomVerouil==1){images->recalZoo=1;}
		
		images->ReafficherZ=1;
	}
		break;
	

	break;

	case  W_KEYPRESS:
	 c = WGetKeyboard();
      switch(c)
	{
	  case 'q':
	 /* Restore image without red line */
		WRestoreImageWindow(images->ImageWindow,0,0,2*nx,ny); 
	    WFlushWindow(images->ImageWindow);
		
		images->Reafficher=1;
		images->ZoomWindow=NULL;
		ret=-1;
		break;

	  
	case XK_Left:case XK_KP_Left:
		
	if(images->ima1||(!images->ZoomVerouil))    images->zoomX1=images->zoomX1-20/images->coeffzoom;
	if((!images->ima1)||(!images->ZoomVerouil)) images->zoomX12=images->zoomX12-20/images->coeffzoom2;

	reaffXY(images);

		x1=images->zoomX1;
		y1=images->zoomY1;



		/* Draw zoom place */		
			dessinerZoneZoom(images->ImageWindow,images);

		/* Compute image */
	 
		petitZoom(images);
		
		if(!images->ZoomVerouil==1){images->recalZoo=1;}
		images->ReafficherZ=1;

			break;
	case XK_Right:case XK_KP_Right:


	if(images->ima1||(!images->ZoomVerouil))    images->zoomX1=images->zoomX1+20/images->coeffzoom;
	if((!images->ima1)||(!images->ZoomVerouil)) images->zoomX12=images->zoomX12+20/images->coeffzoom2;

	reaffXY(images);
		x1=images->zoomX1;
		y1=images->zoomY1;

		/* Draw zoom place */		
			dessinerZoneZoom(images->ImageWindow,images);

		/* Compute image */
	 
		petitZoom(images);
		
		if(!images->ZoomVerouil==1){images->recalZoo=1;}
		images->ReafficherZ=1;

			break;
	case XK_Up:case XK_KP_Up:

	if(images->ima1||(!images->ZoomVerouil))    images->zoomY1=images->zoomY1-20/images->coeffzoom;
	if((!images->ima1)||(!images->ZoomVerouil)) images->zoomY12=images->zoomY12-20/images->coeffzoom2;

	reaffXY(images);
		x1=images->zoomX1;
		y1=images->zoomY1;

		/* Draw zoom place */		
			dessinerZoneZoom(images->ImageWindow,images);

		/* Compute image */
	 
		petitZoom(images);
		
		if(!images->ZoomVerouil==1){images->recalZoo=1;}
		images->ReafficherZ=1;

			break;
	case XK_Down:case XK_KP_Down:
	
	if(images->ima1||(!images->ZoomVerouil))    images->zoomY1=images->zoomY1+20/images->coeffzoom;
	if((!images->ima1)||(!images->ZoomVerouil)) images->zoomY12=images->zoomY12+20/images->coeffzoom2;

	reaffXY(images);

		x1=images->zoomX1;
		y1=images->zoomY1;

		/* Draw zoom place */		
			dessinerZoneZoom(images->ImageWindow,images);

		/* Compute image */
	 
		petitZoom(images);
		
		if(!images->ZoomVerouil==1){images->recalZoo=1;}
		images->ReafficherZ=1;

			break;

	case ' ':
if(images->deuxIm==1){
			if(images->ima1){
				images->ima1=0;
			f2cimageLoc(images->imWind2,images->imageDisp);

			}else{
				images->ima1=1;
			f2cimageLoc(images->imWind,images->imageDisp);
	
			}
			if(images->ZoomWindow) {
				if(!images->ZoomVerouil&&images->recalZoo){				
		/* Draw zoom place */		 
			petitZoom(images);
			images->recalZoo=0;
			}else{
			if(images->ima1){ 
			f2cimageLoc(images->fZoom,images->imaZoomDisp);
			}else{
			f2cimageLoc(images->fZoom2,images->imaZoomDisp);}		
			}
			}
	if(images->SubPlotWindow){
	x0=images->SubPlotWindow->x;
	y0=images->SubPlotWindow->y;
		if(images->ima1){
	      splot3(images->subsection,&x0,&y0,&sx,&sy,
		  &norefresh,(char *)images->SubPlotWindow,NULL,NULL);
		}else{
	      splot3(images->subsection2,&x0,&y0,&sx,&sy,
		  &norefresh,(char *)images->SubPlotWindow,NULL,NULL);
		}
	}
	if(images->PlotWindow){
	x0=images->PlotWindow->x;
	y0=images->PlotWindow->y;
		if(images->ima1){
	      splot3(images->section,&x0,&y0,&sx,&sy,
		  &norefresh,(char *)images->PlotWindow,NULL,NULL);
		}else{
	      splot3(images->section2,&x0,&y0,&sx,&sy,
		  &norefresh,(char *)images->PlotWindow,NULL,NULL);
		}
	}
		/* Draw zoom place */		

			/* Témoin de changement d'affichage */
				images->Reafficher=1;
				images->ReafficherZ=1;
		
}
	  break;



	case 'v':
		if(images->ZoomVerouil) {
			images->ZoomVerouil=0;
		    images->recalZoo=1;
			printf("Zoom Deverouille\n");
			if(images->ima1==1){
				images->zoomX12=images->zoomX1* images->zoomOr /images->zoomOr2;
				images->zoomY12=images->zoomY1* images->zoomOr /images->zoomOr2;
			}else{
				images->zoomX1=images->zoomX12* images->zoomOr /images->zoomOr2;
				images->zoomY1=images->zoomY12* images->zoomOr /images->zoomOr2;
			}
		}else{
		    images->recalZoo=0;
			images->ZoomVerouil=1;
			printf("Zoom Verouille\n");
		}
	break;

		case 'l' :
			if(x1>=0&&x1<nx&&y1<ny&&y1>=0){
	     /* Draw an horizontal red line */
	      WSetTypePencil(W_COPY);
	      WSetSpecialColorPencil(images->ZoomWindow);
	      
		  Affichage(images->imaZoomDisp,nx,ny,images->ZoomWindow);
	      WDrawLine(images->ZoomWindow,0,y1,nx-1,y1);  
	      WFlushWindow(images->ZoomWindow);	  
	      cscale_shown = 0;
	      

	      /* Compute and plot section */
	      if (images->SubPlotWindow != NULL) {x0=images->SubPlotWindow->x;y0=images->SubPlotWindow->y;}
	      else 
			{
		  x0 = images->ZoomWindow->x + nx ;
		  y0 = images->ZoomWindow->y + 2 ;
		  if (y0 < 0) y0 = 0;
	      images->SubPlotWindow = (Wframe *)
		  mw_get_window(images->SubPlotWindow,sx,sy,x0,y0,"");
			}



	if(y1<images->fZoom->nrow){
		fline_extract((char *) NULL, images->fZoom, images->subsection,y1);
	}else{
		mw_clear_fsignal(images->subsection,0.);}
	if(images->deuxIm==1&&y1<images->fZoom2->nrow) {
		fline_extract((char *) NULL, images->fZoom2, images->subsection2,y1);
	}else{
		mw_clear_fsignal(images->subsection2,0.);}


	      sprintf(images->subsection->name,"[ZOOMED] Plot a section");
	      sprintf(images->subsection2->name,"[ZOOMED] Plot a section");

		if(images->ima1){
	      splot3(images->subsection,&x0,&y0,&sx,&sy,
		  &norefresh,(char *)images->SubPlotWindow,NULL,NULL);
		}else{
	      splot3(images->subsection2,&x0,&y0,&sx,&sy,
		  &norefresh,(char *)images->SubPlotWindow,NULL,NULL);
		}

	      /* Restore image without red line */
	      WRestoreImageWindow(images->ZoomWindow,0,0,nx,ny); 
	      WFlushWindow(images->ZoomWindow);
	    	}


	  	  break;

		case 'c' :
	  		
			if(x1>=0&&x1<nx&&y1<ny&&y1>=0){
	     /* Draw an horizontal red line */
	      WSetTypePencil(W_COPY);
	      WSetSpecialColorPencil(images->ZoomWindow);
	      
		  Affichage(images->imaZoomDisp,nx,ny,images->ZoomWindow);
	      WDrawLine(images->ZoomWindow,x1,0,x1,ny-1);  
	      WFlushWindow(images->ZoomWindow);	  
	      cscale_shown = 0;
	      

	      /* Compute and plot section */
	      if (images->SubPlotWindow != NULL) {x0=images->SubPlotWindow->x;y0=images->SubPlotWindow->y;}
	      else 
			{
		  x0 = images->ZoomWindow->x + nx ;
		  y0 = images->ZoomWindow->y + 2 ;
		  if (y0 < 0) y0 = 0;
	      images->SubPlotWindow = (Wframe *)
		  mw_get_window(images->SubPlotWindow,sx,sy,x0,y0,"");
			}


	if(x1<images->fZoom->ncol){
		fline_extract(&flag, images->fZoom, images->subsection,x1);
	}else{
		mw_clear_fsignal(images->subsection,0.);}
	if(images->deuxIm==1&&x1<images->fZoom2->ncol) {
		fline_extract(&flag, images->fZoom2, images->subsection2,x1);
	}else{
		mw_clear_fsignal(images->subsection2,0.);}


	      sprintf(images->subsection->name,"[Zoomed]Plot a section");
	      sprintf(images->subsection2->name,"[Zoomed]Plot a section");

		if(images->ima1){
	      splot3(images->subsection,&x0,&y0,&sx,&sy,
		  &norefresh,(char *)images->SubPlotWindow,NULL,NULL);
		}else{
	      splot3(images->subsection2,&x0,&y0,&sx,&sy,
		  &norefresh,(char *)images->SubPlotWindow,NULL,NULL);
		}

	      /* Restore image without red line */
	      WRestoreImageWindow(images->ZoomWindow,0,0,nx,ny); 
	      WFlushWindow(images->ZoomWindow);
	    	}
	    
	  break;

	  case 'h': case 'H': zoom_notify_help();
	  	break;	

	  case 'd':  case W_MS_MIDDLE:
		images->GLprintZ=1-images->GLprintZ;
		if(images->GLprintZ==0)images->ReafficherZ=1;

	break;


	}
	break;
	}


	return(ret);
}



/*     A notify function must return a value ....      */
/*       0 if there was no event catched               */
/*     > 0 if there was an event catched (but Destroy) */
/*      -1 if the event Destroy was catched (or 'Q')   */

static int hfv_notify(ImageWindow,param)

Wframe *ImageWindow;
void *param;          /* Users's parameters: don't forget the cast ! */

{
	/* Initialisations */
	int x0,y0,x1,y1,event,button_mask,ret,c,nx,ny;
  	char flag;
	double ifx,ify;
  	hfv_Param images;
	float ng;
	char mess[80];
	int vraiX,vraiY;

	x0=0;y0=0;
  	images = (hfv_Param) param;  /* Cast */	
	nx=images->imageRe->ncol;
	ny=images->imageRe->nrow;
	

	/* For section */
  	int sx=500;
  	int sy=200;
  	char cflag=1;
  	int norefresh=1;
	


	/* Captation de l'evenement du moment */
  	event = WUserEvent(ImageWindow); /* User's event on ImageWindow */	
  	if (event < 0) ret=1; else ret=event;
	
  	/* Affichage de l'interface */
	if (event != W_DESTROY)
     {
	    WGetStateMouse(ImageWindow,&x1,&y1,&button_mask);	 



	 }




	if(images->Reafficher==1){

		Affichage(images->imageDisp,images->imageDisp->ncol,images->imageDisp->nrow,ImageWindow);	
		if(images->ZoomWindow) dessinerZoneZoom(images->ImageWindow,images);
		images->Reafficher=0;
	}

	/* Affichage si changement */
      if (images->GLprint == 1)  
	{
	if(images->ima1){
	  vraiX=(int)(x1/images->zoomOr);
	  vraiY=(int)(y1/images->zoomOr);

	  if ((vraiX>=0)&&(vraiX<images->imageRe->ncol)
		&&(vraiY>=0)&&(vraiY<images->imageRe->nrow))
	    {
	      ng = images->imageRe->gray[ vraiY + (images->imageRe->ncol)* vraiX ];
	      sprintf(mess," (%3d,%3d): %f ",vraiX,vraiY,ng);
	    }
	  else sprintf(mess," (%3d,%3d): ----------",vraiX,vraiY);
	}else{
	  vraiX=(int)(x1/images->zoomOr2);
	  vraiY=(int)(y1/images->zoomOr2);	
	  if ((vraiX>=0)&&(vraiX<images->imageRe2->ncol)
		&&(vraiY>=0)&&(vraiY<images->imageRe2->nrow))
	    {
	      ng = images->imageRe2->gray[ vraiY + (images->imageRe2->ncol)* vraiX ];
	      sprintf(mess," (%3d,%3d): %f ",vraiX,vraiY,ng);
	    }
	  else sprintf(mess," (%3d,%3d): ----------",vraiX,vraiY);
	}
	  WDrawString(ImageWindow,0,10,mess);
	  WFlushAreaWindow(ImageWindow,0,0,images->imageDisp->ncol-1,12);
	  /*usleep(100);*/
	}


	/* Gestion de tous les évènements */					   
  switch(event)
    {


    case W_MS_LEFT:

      images->GLprint = 1 - images->GLprint;
     if (images->GLprint == 0) images->Reafficher=1; 


      break;
      


	case  W_KEYPRESS:
	 c = WGetKeyboard();
      switch(c)
	{
	/* Filp */
	case ' ':
if(images->deuxIm==1){
			if(images->ima1){
				images->ima1=0;
			f2cimageLoc(images->imWind2,images->imageDisp);

			}else{
				images->ima1=1;
			f2cimageLoc(images->imWind,images->imageDisp);
	
			}
			if(images->ZoomWindow) {
				if(!images->ZoomVerouil&&images->recalZoo){				
		/* Draw zoom place */		 
			petitZoom(images);
			images->recalZoo=0;
			}else{
			if(images->ima1){ 
			f2cimageLoc(images->fZoom,images->imaZoomDisp);
			}else{
			f2cimageLoc(images->fZoom2,images->imaZoomDisp);}		
			}
			}
	if(images->SubPlotWindow){
	x0=images->SubPlotWindow->x;
	y0=images->SubPlotWindow->y;
		if(images->ima1){
	      splot3(images->subsection,&x0,&y0,&sx,&sy,
		  &norefresh,(char *)images->SubPlotWindow,NULL,NULL);
		}else{
	      splot3(images->subsection2,&x0,&y0,&sx,&sy,
		  &norefresh,(char *)images->SubPlotWindow,NULL,NULL);
		}
	}
	if(images->PlotWindow){
	x0=images->PlotWindow->x;
	y0=images->PlotWindow->y;
		if(images->ima1){
	      splot3(images->section,&x0,&y0,&sx,&sy,
		  &norefresh,(char *)images->PlotWindow,NULL,NULL);
		}else{
	      splot3(images->section2,&x0,&y0,&sx,&sy,
		  &norefresh,(char *)images->PlotWindow,NULL,NULL);
		}
	}
		/* Draw zoom place */		

			/* Témoin de changement d'affichage */
				images->Reafficher=1;
				images->ReafficherZ=1;
		
}
	  break;
	   /*Déplacement avec le pavé de la zone de filtrage de l'alias */
	case XK_Right: case XK_KP_Right: 



		break;

	  
	case XK_Left:case XK_KP_Left:



		break;


	case XK_Up: case XK_KP_Up:
	

		break;

	 
	case XK_Down: case XK_KP_Down:



	 	break; 

	  case 'h': case 'H': hfv_notify_help();
	  	break;	
	  case 'q':
		ret=-1;
			break;

		case 't':

			
		break;
		case 'g':


		break;

		case 'e':

			
		break;

	case 'v':
		if(images->ZoomVerouil) {
			images->ZoomVerouil=0;
		    images->recalZoo=1;
			printf("Zoom Deverouille\n");
			if(images->ima1==1){
				images->zoomX12=images->zoomX1* images->zoomOr /images->zoomOr2;
				images->zoomY12=images->zoomY1* images->zoomOr /images->zoomOr2;
			}else{
				images->zoomX1=images->zoomX12* images->zoomOr /images->zoomOr2;
				images->zoomY1=images->zoomY12* images->zoomOr /images->zoomOr2;
			}
		}else{
		    images->recalZoo=0;
			images->ZoomVerouil=1;
			printf("Zoom Verouille\n");
		}
	break;
				
			
	  case 'f':

			break;
		case'r':

			break;
		case 's':

	
			break;
		case 'd':

		break;

		case 'z':

	if((images->ZoomVerouil)){
	if(images->ima1){
		images->zoomX1=(int)x1*1./images->zoomOr;
		images->zoomY1=(int)y1*1./images->zoomOr;
	}	
	if(!(images->ima1)){
		images->zoomX12=(int)x1*1./images->zoomOr2;
		images->zoomY12=(int)y1*1./images->zoomOr2;	
	}
	}else{
		if(images->zoomOr/images->zoomOr2<1.){
		images->zoomX12=(int)x1*1./images->zoomOr2;
		images->zoomY12=(int)y1*1./images->zoomOr2;
		images->zoomX1=(int)images->zoomX12*images->zoomOr2/images->zoomOr;
		images->zoomY1=(int)images->zoomY12*images->zoomOr2/images->zoomOr;	

		}else{
		images->zoomX1=(int)x1*1./images->zoomOr;
		images->zoomY1=(int)y1*1./images->zoomOr;
		images->zoomX12=(int)images->zoomX1*images->zoomOr/images->zoomOr2;
		images->zoomY12=(int)images->zoomY1*images->zoomOr/images->zoomOr2;
		}
	}

	if(images->zoomX1<0){
		images->zoomX1=images->zoomX12*images->zoomOr2/images->zoomOr;
		images->zoomY1=images->zoomY12*images->zoomOr2/images->zoomOr;
		if(images->ima1==0){
			images->ima1=1;
			petitZoom(images);
			images->ima1=0;
		}
	}
	if(images->zoomX12<0){
		images->zoomX12=images->zoomX1*images->zoomOr/images->zoomOr2;
		images->zoomY12=images->zoomY1*images->zoomOr/images->zoomOr2;
		if(images->ima1==1){
			images->ima1=0;
			petitZoom(images);
			images->ima1=1;
		}
	}

	reaffXY(images);

	
	

	images->ReafficherZ=0;

		/* Draw zoom place */		
			dessinerZoneZoom(images->ImageWindow,images);
	      


		/* Compute image */
	

	      if (images->ZoomWindow != NULL) {x0=images->ZoomWindow->x;y0=images->ZoomWindow->y;

			}else {

		  x0 = images->ImageWindow->x+20  ;
		  y0 = images->ImageWindow->y+20;
		  if (y0 < 0) y0 = 0;
	      images->ZoomWindow = (Wframe *)
	  	  mw_get_window(images->ZoomWindow,images->nZoomX,images->nZoomY,x0,y0,"Zoom");
			}

		 
		petitZoom(images);
		



		WSetUserEvent(images->ZoomWindow,W_MS_BUTTON | W_KEYPRESS);

		if(!images->ZoomVerouil==1){images->recalZoo=1;}
		images->ReafficherZ=1;

		mw_window_notify(images->ZoomWindow,(void *)images,zoom_notify);
	
	    


		break;

		case 'l' :
			if(x1>=0&&x1<images->imageDisp->ncol&&y1<images->imageDisp->nrow&&y1>=0){
	     /* Draw an horizontal red line */
	      WSetTypePencil(W_COPY);
	      WSetSpecialColorPencil(images->ImageWindow);
	      
		  Affichage(images->imageDisp,nx,ny,ImageWindow);
	      WDrawLine(images->ImageWindow,0,y1,images->imageDisp->ncol-1,y1);  
	      WFlushWindow(images->ImageWindow);	  
	      cscale_shown = 0;
	      

	      /* Compute and plot section */
	      if (images->PlotWindow != NULL) {x0=images->PlotWindow->x;y0=images->PlotWindow->y;}
	      else 
			{
		  x0 =images-> ImageWindow->x + x1 ;
		  y0 = images->ImageWindow->y + y1 ;
		  if (y0 < 0) y0 = 0;
	      images->PlotWindow = (Wframe *)
		  mw_get_window(images->PlotWindow,sx,sy,x0,y0,"");
			}


	if((int)((float) y1/images->zoomOr)<images->imageRe->nrow){
		fline_extract((char *) NULL, images->imageRe, images->section,(int)((float) y1/images->zoomOr));
	}else{
		mw_clear_fsignal(images->section,0.);}
	if(images->deuxIm==1&&(int)((float) y1/images->zoomOr2)<images->imageRe2->nrow) {
		fline_extract((char *) NULL, images->imageRe2, images->section2,(int)((float) y1/images->zoomOr2));}else{mw_clear_fsignal(images->section2,0.);}


	      sprintf(images->section->name,"Plot a section");
	      sprintf(images->section2->name,"Plot a section");


		if(images->ima1){
	      splot3(images->section,&x0,&y0,&sx,&sy,
		  &norefresh,(char *)images->PlotWindow,NULL,NULL);
		}else{
	      splot3(images->section2,&x0,&y0,&sx,&sy,
		  &norefresh,(char *)images->PlotWindow,NULL,NULL);
		}

	      /* Restore image without red line */
	      WRestoreImageWindow(images->ImageWindow,0,0,images->imageDisp->ncol,images->imageDisp->nrow); 
	      WFlushWindow(images->ImageWindow);
	    	}


	  	  break;
	    
		case 'c' :
	  		
			if(x1>=0&&x1<images->imageDisp->ncol&&y1<images->imageDisp->nrow&&y1>=0){
	     /* Draw an horizontal red line */
	      WSetTypePencil(W_COPY);
	      WSetSpecialColorPencil(images->ImageWindow);
	      
		  Affichage(images->imageDisp,nx,ny,images->ImageWindow);
	      WDrawLine(images->ImageWindow,x1,0,x1,images->imageDisp->nrow-1);  
	      WFlushWindow(images->ImageWindow);	  
	      cscale_shown = 0;
	      

	      /* Compute and plot section */
	      if (images->PlotWindow != NULL) {x0=images->PlotWindow->x;y0=images->PlotWindow->y;}
	      else 
			{
		  x0 = images->ImageWindow->x + x1 ;
		  y0 = images->ImageWindow->y + y1 ;
		  if (y0 < 0) y0 = 0;
	      images->PlotWindow = (Wframe *)
		  mw_get_window(images->PlotWindow,sx,sy,x0,y0,"");
			}


	if((int)((float) x1/images->zoomOr)<images->imageRe->ncol){
		fline_extract(&flag, images->imageRe, images->section,(int)((float) x1/images->zoomOr));
	}else{
		mw_clear_fsignal(images->section,0.);}
	if(images->deuxIm==1&&(int)((float) x1/images->zoomOr2)<images->imageRe2->ncol) {
		fline_extract(&flag, images->imageRe2, images->section2,(int)((float) x1/images->zoomOr2));
	}else{
		mw_clear_fsignal(images->section2,0.);}

	      sprintf(images->section->name,"Plot a section");
	      sprintf(images->section2->name,"Plot a section");

		if(images->ima1){
	      splot3(images->section,&x0,&y0,&sx,&sy,
		  &norefresh,(char *)images->PlotWindow,NULL,NULL);
		}else{
	      splot3(images->section2,&x0,&y0,&sx,&sy,
		  &norefresh,(char *)images->PlotWindow,NULL,NULL);
		}

	      /* Restore image without red line */
	      WRestoreImageWindow(images->ImageWindow,0,0,images->imageDisp->ncol,images->imageDisp->nrow); 
	      WFlushWindow(images->ImageWindow);
	    	}
	    
	  break;	
			
	}
	}

/*mw_delete_fsignal(section);*/
 /*    if(ret== -1)free(images);*/

  return(ret);
}





/* Main loop */

void hugeFview(in,in2,zoom,zoom2,window,order,dee,nZoomX,nZoomY,sameSize,tailImp)
	Fimage in,in2;
    char   *window,*sameSize;
	float *zoom,*zoom2,*dee;
	int *order,*tailImp;
	int *nZoomX,*nZoomY;

{
	/* Initialisations */
	int x0,y0;
	char flag;
	x0=50;
	y0=50;	
    Fimage tmp,tmp2;
	float inverse_zoom;
	float min,max;min=0.;max=255.;

	inverse_zoom=1;
	hfv_Param param;

	param = (hfv_Param) malloc(sizeof(struct hfv_SParam));
    if (param == NULL) mwerror(FATAL,1,"not enough memory\n");

	if(in2!=NULL){param->deuxIm=1;}else{param->deuxIm=0;}
	
if(tailImp){


	if(in->ncol>in->nrow){
	*zoom=(float)*tailImp/(float)in->ncol;
	}else{
	*zoom=(float)*tailImp/(float)in->nrow;
	}
	

}

	/* Zoom */
	if (*zoom != 1.0) {
      	tmp = mw_change_fimage(NULL,0,0);
		
		if(*order==12){
		fftzoom(in,tmp,zoom,NULL);		
		}else{


	    if (tmp == NULL) mwerror(FATAL,1,"Not enough memory\n");
    	if (*zoom>1.0) {
			fzoom(in,tmp,NULL,NULL,zoom,order,NULL,NULL);
    	} else {
		inverse_zoom = 1./(*zoom);

		fzoom(in,tmp,NULL,NULL,&inverse_zoom,order,NULL,(char *)1);
		

      	}

}

      	sprintf(tmp->name,"%s %.1fX",in->name,*zoom);
    } else {
	  	tmp=mw_change_fimage(NULL,in->nrow,in->ncol);
      	mw_copy_fimage(in,tmp);
  	}



	if(param->deuxIm==1){

if((*zoom2==1.0&&*zoom==1.0)||sameSize){

	if(in2->ncol>in2->nrow){
	*zoom2=(float)in->ncol/(float) in2->ncol;
	if(in2->nrow* *zoom2>in->nrow) *zoom2=(float)in->nrow/(float) in2->nrow;
	*zoom2=*zoom2* *zoom;
	}else{
	*zoom2=(float)in->nrow/(float) in2->nrow;
	if(in2->ncol* *zoom2>in->ncol) *zoom2=(float)in->ncol/(float) in2->ncol;
	*zoom2=*zoom2* *zoom;
	}
	

}
		if (*zoom2 != 1.0) {
 			tmp2 = mw_change_fimage(NULL,0,0);

		if(*order==12){
		fftzoom(in2,tmp2,zoom2,NULL);		
		}else{

    		if (*zoom2>1.0) {
				fzoom(in2,tmp2,NULL,NULL,zoom2,order,NULL,NULL);
    		} else {
			inverse_zoom = 1./(*zoom2);
			fzoom(in2,tmp2,NULL,NULL,&inverse_zoom,order,NULL,(char *)1);
      		}
		}
			sprintf(tmp2->name,"%s %.1fX",in2->name,*zoom);
    	} else {
		tmp2=mw_change_fimage(NULL,in2->nrow,in2->ncol);
      	mw_copy_fimage(in2,tmp2);
  		}

	}


	/* Ajustement pour images de meme taille */
	int tailX,tailY;

	if(param->deuxIm==1){tailX=tmp->ncol>tmp2->ncol?tmp->ncol:tmp2->ncol;}else{tailX=tmp->ncol;}
	if(param->deuxIm==1){tailY=tmp->nrow>tmp2->nrow?tmp->nrow:tmp2->nrow;}else{tailY=tmp->nrow;}
	param->tailX=tailX;param->tailY=tailY;

 
	param->imWind=mw_change_fimage(NULL,tailY,tailX);

	if(param->deuxIm==1){param->imWind2=mw_change_fimage(NULL,tailY,tailX);}
	mw_clear_fimage(param->imWind,0.);	

	if(param->deuxIm==1){
	mw_clear_fimage(param->imWind2,0.);
	if(tmp->nrow!=tmp2->nrow||tmp->ncol!=tmp2->ncol){
	param->ZoomVerouil=1;
		int x,y;
		for(x=0;x<tmp->ncol;x++)
			for(y=0;y<tmp->nrow;y++){
			param->imWind->gray[y*tailX+x]=tmp->gray[y*tmp->ncol+x];
		}
		for(x=0;x<tmp2->ncol;x++)
			for(y=0;y<tmp2->nrow;y++){		
			param->imWind2->gray[y*tailX+x]=tmp2->gray[y*tmp2->ncol+x];
		}
	}else{
	param->ZoomVerouil=0;
	mw_copy_fimage(tmp,param->imWind);
	mw_copy_fimage(tmp2,param->imWind2);
	}
	}else{
	mw_copy_fimage(tmp,param->imWind);
	}


 	/* PARAM : Fimage d'entrée (non normalisée) */
	param->imageRe=mw_change_fimage(NULL,in->nrow,in->ncol);
	param->imageReN=mw_change_fimage(NULL,in->nrow,in->ncol);
	mw_copy_fimage(in,param->imageRe);
	if(param->deuxIm==1){
	param->imageRe2=mw_change_fimage(NULL,in2->nrow,in2->ncol);
	param->imageReN2=mw_change_fimage(NULL,in2->nrow,in2->ncol);
	mw_copy_fimage(in2,param->imageRe2); }
	


    fthre(param->imageRe,param->imageReN,&flag,NULL,&min,&max,NULL,NULL,dee,NULL,NULL);
    fthre(param->imWind,param->imWind,&flag,NULL,&min,&max,NULL,NULL,dee,NULL,NULL);

    if(param->deuxIm==1){ 
	fthre(param->imageRe2,param->imageReN2,&flag,NULL,&min,&max,NULL,NULL,dee,NULL,NULL);
    fthre(param->imWind2,param->imWind2,&flag,NULL,&min,&max,NULL,NULL,dee,NULL,NULL);
	}
	
	int nx,ny,nx2,ny2;			
	nx=param->imageRe->ncol;
	ny=param->imageRe->nrow; 
	param->nx=nx;
	param->ny=ny;
	if(param->deuxIm==1){	nx2=param->imageRe2->ncol;
	ny2=param->imageRe2->nrow; 
	param->nx2=nx2;
	param->ny2=ny2;}
	param->nZoomX=*nZoomX;
	param->nZoomY=*nZoomY;
	param->zoomOr=*zoom;
	param->zoomOr2=*zoom2;
	param->fZoom=mw_change_fimage(NULL,param->nZoomY,param->nZoomX);
	param->fZoom2=mw_change_fimage(NULL,param->nZoomY,param->nZoomX);
	param->ima1=1;

	

	param->imageDisp=mw_change_cimage(NULL,param->imWind->nrow,param->imWind->ncol);
	f2cimageLoc(param->imWind,param->imageDisp);


/* Création de la fenetre */
	param->ImageWindow = (Wframe *)
    mw_get_window((Wframe *) window,param->imWind->ncol,param->imWind->nrow,x0,y0,in->name);
	
	if (param->ImageWindow == NULL)
    mwerror(INTERNAL,1,"NULL window returned by mw_get_window\n");
  	
  	WFlushWindow(param->ImageWindow);

	/* Premier affichage */
	Affichage(param->imageDisp,param->imWind->ncol,param->imWind->nrow,param->ImageWindow);

	
	oldx1 = oldy1 = oldevent = -1;
    zfactor = *zoom;
	cscale_shown = 0;
	


	param->imaZoomDisp=mw_change_cimage(NULL,param->nZoomY,param->nZoomX);


	/* PARAM : les autres parametres */
	param->posX1=0;
	param->posY1=0;
	param->AposX1=0;
	param->AposY1=0;
	param->avX=0;
	param->avY=0;
	param->pasFiltres=1;
	param->imaZoomDisp=mw_change_cimage(NULL,param->nZoomY,param->nZoomX);
	param->zoomX12=-1;
	param->zoomY12=-1;
	param->zoomX1=-1;
	param->zoomY1=-1;
	param->recalZoo=0;
	
	param->coeffzoom=8.* *zoom;
	param->coeffzoom2=8.* *zoom* *zoom2/ *zoom;	

	param->section=mw_change_fsignal(NULL,(nx < ny)?ny:nx);
	param->subsection=mw_change_fsignal(NULL,(nx < ny)?ny:nx);
	param->section2=mw_change_fsignal(NULL,(nx < ny)?ny:nx);
	param->subsection2=mw_change_fsignal(NULL,(nx < ny)?ny:nx);
param->nx=in->ncol;
	param->ny=in->nrow;
	param->GLprint=0;
	param->GLprintZ=0;
	param->dynRecalZoo=0;
	param->tailChange=0;
	

	param->ZoomWindow=NULL;
	param->PlotWindow=NULL;									
	param->SubPlotWindow=NULL;								 						

	/* Lancement de la boucle */
	WSetUserEvent(param->ImageWindow,W_MS_BUTTON | W_KEYPRESS);
	mw_window_notify(param->ImageWindow,(void *)param,hfv_notify);
	mw_window_main_loop();
  	mw_delete_fimage(tmp);




}
